package com.yuwubao.hz_International.quartz

import com.yuwubao.hz_International.entities.AcountDetailEntity
import com.yuwubao.hz_International.entities.ConsumerEntity
import com.yuwubao.hz_International.entities.GiftConfigEntity
import com.yuwubao.hz_International.services.AppService
import com.yuwubao.hz_International.services.impl.retain
import com.yuwubao.hz_International.util.TypeConst
import org.slf4j.LoggerFactory
import org.springframework.scheduling.annotation.Scheduled
import org.springframework.stereotype.Component
import java.math.BigDecimal
import java.util.*
import javax.annotation.Resource

/**
 * 个人福利金收益
 */
@Component
class ConsumerWelfareQuartz {
    private val logger = LoggerFactory.getLogger(ConsumerWelfareQuartz::class.java)
    @Resource
    private lateinit var appService: AppService


    /**
     * 每天0天生成昨日收益
     */
    @Scheduled(cron = "0 0  0 * * ?")
    fun revenue() {
        val dayRate = appService.welfareConfig()?.dayRate
        val hasWelfareUser: List<ConsumerEntity> = appService.hasWelfareUser()
        hasWelfareUser.map { it ->
            logger.info("用户：${it.phone}")
            val welfare = it.welfareMoney
            val yesterday = retain(welfare.multiply(dayRate).divide(BigDecimal(10000)))
            it.welfareYesterdayMoney = yesterday
        }
        appService.saveUser(hasWelfareUser)
    }

    /**
     * 每天23点30增加收益
     */
    @Scheduled(cron = "0 30 23 * * ?")
    fun addRevenue() {
        val hasWelfareUser: List<ConsumerEntity> = appService.hasWelfareUser()
        val list = mutableListOf<AcountDetailEntity>()
        hasWelfareUser.map { it ->
            val yesterday = it.welfareYesterdayMoney
            it.welfareProfitMoney = it.welfareProfitMoney.add(yesterday)
            it.welfareMoney = it.welfareMoney.add(yesterday)

            val detail = AcountDetailEntity()
            detail.consumerId = it.id
            detail.currentGift = it.giftMoney
            detail.currentWelfare = it.welfareMoney
            detail.currentBonous = it.bonous
            detail.currentMoney = it.money
            detail.payType  = 0
            detail.type = TypeConst.coin.welfare
            detail.money = "0"
            detail.gift = "0"
            detail.welfare = "+"+yesterday
            detail.bonouse = "0"
            detail.useType = TypeConst.coinUseType.sys_auto
            detail.createTime = Date()
            list.add(detail)
        }
        appService.saveUser(hasWelfareUser)
        appService.saveDetail(list)
    }

    /**
     * 每天积分自动转礼券
     */
    @Scheduled(cron = "0 0 0 * * ?")
    fun transGift() {
//        val hasGiftUser: List<ConsumerEntity> = appService.hasGiftUser()
        val hasBonousUser :List<ConsumerEntity> = appService.hasBonousUser()
        val giftConfig: GiftConfigEntity? = appService.giftConfig()
        val rate = giftConfig?.glft
        val list = mutableListOf<AcountDetailEntity>()
        hasBonousUser.map { it ->
            val bonous = it.bonous
            val transMoney = retain(bonous.multiply(rate).divide(BigDecimal(10000)))
            it.bonous = it.bonous.subtract(transMoney)
            it.giftMoney = it.giftMoney.add(transMoney)
            val detail = AcountDetailEntity()
            detail.consumerId = it.id
            detail.currentMoney = it.money
            detail.currentBonous = it.bonous
            detail.currentWelfare = it.welfareMoney
            detail.currentGift = it.giftMoney
            detail.payType = 0
            detail.type = TypeConst.coin.integral
            detail.gift = "+" + transMoney
            detail.bonouse = "-" + transMoney
            detail.welfare = "0"
            detail.money = "0"
            detail.useType = TypeConst.coinUseType.sys_auto
            detail.createTime = Date()
            list.add(detail)
            it
        }
        appService.saveUser(hasBonousUser)
        appService.saveDetail(list)
    }


}
